# Feel free to add "#! /bin/sh" to the top of the script. I prefer
# it without...
#
# Filename      : clonea.sh
# Author        : Christopher Steel
# Organization  : "Fair Access" not for profit in process of incorporation.
# Date          : 2010-02-26
# License       : MIT
#
# If you have feedback, suggestions, improvments or other scripts / information
# you feel would be helpful you can contact me at chris dot steel
# at gmail dot com for the time being.... Thanks!
#
# Metadata:
#
# Development
# Mercurial
# Web2py
# Universal Cake
#
# Tested on:
#
#		- Ubuntu
#			- 8.04 Hardy
#		- OSX
#			- 10.4.11
# should work "everywhere" bash is available.
#
# Description:
#
# This script makes a local clone of a googlecode repository and backsup
# any existing respository to prevent clobbering. Currently it is customised
# for projects beginning with "uc" but this can be easily removed by getting
# rid of the uc_ and uc- in the LOCAL_APP_NAME and G_APP_NAME variables.
#
# Notes:
#
# Google code does not allow underscore in names and web2py does not recognize
# applications with hyphens "-". Therefore we require two different names,
# one with the hyphen,gcode_feature_name and one using underscore local_feature_name.
#
# TODO: Add logging and/or logging script add on.
#
SCRIPT_NAME=`basename ${0} .sh`                             # script name with no .sh at the end

#######################
# Repository Variables
#######################
APP_NAME=${1}
#
# You probably want to adjust the following two variables
# to match your projects naming scheme.
#
LOCAL_APP_NAME=uc_${APP_NAME}         # local app name with prefix of _uc
G_APP_NAME=uc-${APP_NAME}                  # remote (google code) with prefix -uc

CWD=`pwd`
W2P_WORKING_DIR=${CWD}       # You might want to manually change this to reflect your setup
														# in some rare cases
LOCAL_TARGET_REPOSITORY_URL=${W2P_WORKING_DIR}/${LOCAL_APP_NAME}

DATE_AND_TIME_STAMP=$(date +D_%Y_%m_%d_T_%H_%M_%S) # valid as web2py app title, international,sorts nicely
BACKUP_TAG="${SCRIPT_NAME}_backup"
###################
# Variable Testing
###################
#
if [ "${1}" = "test" ]
    then
    echo "##############################################"
    echo "# Variable Testing"
    echo "##############################################"
    echo "#"
	echo "SCRIPT_NAME = ${SCRIPT_NAME}"
    echo ""
    echo "APP_NAME = ${APP_NAME}"
    echo ""
    echo "LOCAL_APP_NAME = ${LOCAL_APP_NAME}"
    echo "G_APP_NAME = ${G_APP_NAME}"
    echo ""
    echo "W2P_WORKING_DIR = ${W2P_WORKING_DIR}"
    echo "LOCAL_TARGET_REPOSITORY_URL = ${LOCAL_TARGET_REPOSITORY_URL}"
    echo ""
    echo "DATE_AND_TIME_STAMP = ${DATE_AND_TIME_STAMP}"
    echo ""
	echo "BACKUP_TAG = ${BACKUP_TAG}"
    echo "##############################################"
    exit 1
fi
###################
# Usage message
###################
if [ ! $1 ]
then
    echo "##############################################"
    echo "# Usage"
    echo "##############################################"
    echo ""
    echo "${SCRIPT_NAME} requires at least one command"
    echo "line option, 'test' or the name of a project"
    echo ""
    echo "See Notes before using on your project."
    echo "You will need to make a slight modification"
    echo "of the script."
    echo ""
    echo "Usage Example:"
    echo "      ${SCRIPT_NAME} blank"
    echo ""
    echo "Testing Example:"
    echo ""
    echo "      ${SCRIPT_NAME} test"
    echo ""
    echo "NOTES:"
    echo "This script adds the prefix 'uc-' and "
    echo "'uc_' to the project name. If your project"
    echo "names do not start with uc- or uc_ you may"
    echo "change the variables LOCAL_APP_NAME and"
    echo "G_APP_NAME to match your project and features"
    echo "naming scheme."
    echo ""
    echo "##############################################"
    exit 1
fi

###################
# Our Code actions
###################

cd ${W2P_WORKING_DIR}                       # change to our working directory
###############################
# backup any previous versions
###############################
if [ -d ${LOCAL_TARGET_REPOSITORY_URL} ]
then
    mv ${LOCAL_TARGET_REPOSITORY_URL} ${LOCAL_TARGET_REPOSITORY_URL}_${DATE_AND_TIME_STAMP}_${BACKUP_TAG}
    echo "moved ${LOCAL_TARGET_REPOSITORY_URL} to "
    echo "${LOCAL_TARGET_REPOSITORY_URL}_${DATE_AND_TIME_STAMP}_${BACKUP_TAG}"
    # add logging???
fi
########
# Clone 
########
hg clone https://${G_APP_NAME}.googlecode.com/hg/ ${LOCAL_APP_NAME}
############
# That's it
############
echo "completed"

